<?php
/**
 * @version		$Id: livezilla.php 846 2010-03-21 13:24:24Z mic $
 * @package		FileZilla - Module 4 OpenCart - Admin Controller
 * @copyright	(C) 2010 mic [ http://osworx.net ]. All Rights Reserved.
 * @license		http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */

class ControllerModuleLiveZilla extends Controller
{
	private $error		= array();
	private $_name		= '';
	private $_url;
	private $_type		= 'module';
	private $_version	= '1.0.6';

	/**
	 * main function
	 */
	public function index() {
		$this->getBaseSetting();
		$this->load->model( 'setting/setting' );

		$this->_url = HTTPS_SERVER . 'index.php?route=';

		if( ( $this->request->server['REQUEST_METHOD'] == 'POST' ) && ( $this->validate() ) ) {
			$this->model_setting_setting->editSetting( $this->_name, $this->request->post );

			$this->session->data['success'] = $this->language->get( 'text_success' );

			if( isset( $this->request->post['mode'] ) && $this->request->post['mode'] == 'apply' ) {
				$this->redirect( $this->_url . $this->_type .'/'. $this->_name );
			}else{
				$this->redirect( $this->_url . 'extension/module' );
			}
		}

		$this->data['version']	= $this->_version;

		$this->getLinks();
		$this->getParams();
		$this->getTemplate();
		$this->getBreadcrumbs();
		$this->getMessages();
		$this->getFooter();
		$this->getDocument();

		$this->response->setOutput( $this->render( true ), $this->config->get( 'config_compression' ) );
	}

	/**
	 * calls several internal functions to load basic settings/definitions
	 */
	private function getBaseSetting() {
		$this->getName();
		$this->getUrl();
		$this->getLanguage();
	}

	/**
	 * build the internal URL
	 * @since OC 1.4.2
	 */
	private function getUrl() {
		$this->_url = HTTPS_SERVER . 'index.php?route=';
	}

	/**
	 * gets the module name out of the class
	 */
	private function getName() {
        $this->_fName	= str_replace( 'ControllerModule', '', get_class( $this ) );
		$this->_name	= strtolower( $this->_fName );
    }

	/**
	 * - adds javascript and css into document header
	 * - defines document title
	 */
	private function getDocument() {
		$this->document->scripts[]	= 'jquery/tooltip/jquery.tools_tooltips.min.js';
		$this->document->styles[]	= 'tooltip_simple.css';
		$this->document->title		= $this->language->get( 'heading_title' );
	}

	/**
	 * build href links
	 */
	private function getLinks() {
		$this->data['link'] = array(
			'action'	=> $this->_url . $this->_type .'/'. $this->_name,
			'cancel'	=> $this->_url . 'extension/' . $this->_type,
			'help'		=> 'http://osworx.net?task=findkey&amp;keyref=opencart_livezilla'
		);
	}

	/**
	 * build breadcrumbs
	 */
	private function getBreadcrumbs() {
		$this->document->breadcrumbs = array(
			array(
	       		'href'      => $this->_url . 'common/home',
	       		'text'      => $this->language->get( 'text_home' ),
	      		'separator' => false
		  	),
	  		array(
	       		'href'      => $this->_url . 'extension/' . $this->_type,
	       		'text'      => $this->language->get( 'text_module' ),
	      		'separator' => ' :: '
	   		)
 		);
	}

	/**
	 * get locale, active languages
	 */
	private function getLocaleLangs() {
		$this->load->model( 'localisation/language' );
		$this->data['localeLangs'] = $this->model_localisation_language->getLanguages();
	}

	/**
	 * get language vars
	 */
	private function getLanguage() {
		$this->load->language( $this->_type .'/'. $this->_name );
		$this->getLocaleLangs();

		// standard
		$lngStd = array(
			'heading_title',
			'text_enabled', 'text_disabled', 'text_left', 'text_right', 'text_header', 'text_footer',
			'button_save', 'button_cancel',
		 	'entry_position', 'entry_status', 'entry_sort_order', 'entry_yes', 'entry_no'
		);

		//extended
		$lngExt = array(
			'entry_code', 'entry_header', 'entry_title', 'entry_visibility',
			'help_position', 'help_code', 'help_title', 'help_visibility',
			'tab_common', 'tab_advanced',
			'button_help', 'button_apply'
		);

		$langs = array_merge( $lngStd, $lngExt );
		foreach( $langs as $lang ) {
			$this->data[$lang]	= $this->language->get( $lang );
		}

		unset( $lngStd, $lngExt, $langs );
	}

	/**
	 * checks if requested language var exists
	 * return value only if exist and is different then variable name
	 * can be used if some new language vars are used which may not included in other language packs
	 *
	 * this check is because OC 1.4.0 language class returns the variable name instead of value if not exist
	 *
	 * @param string	$var	name of requested variable
	 * @return string/void
	 */
	private function checkLanguage( $var ) {
		$ret = $this->language->get( $var );

		if( $ret != $var) {
			return $ret;
		}else{
			return null;
		}
	}

	/**
	 * get params
	 */
	private function getParams() {
		// standard
		$parmStd = array( 'position', 'status', 'sort_order' );
		// module specific
		$parmExt = array( 'code', 'header', 'title', 'visibility' );
		// merge the arrays
		$params = array_merge( $parmStd, $parmExt );

		foreach( $params as $param ) {
			$this->getParam( '_' . $param );
		}

		// language specific params
		foreach( $this->data['localeLangs'] as $lang ) {
			$this->getParam( '_title' . $lang['language_id'] );
		}

		unset( $parmStd, $parmExt, $params );
	}

	/**
	 * get a value either from request or config
	 */
	private function getParam( $param ) {
		$name = $this->_name . $param;
		if( isset( $this->request->post[$name] ) ) {
			$this->data[$name] = $this->request->post[$name];
		}else{
			$this->data[$name] = $this->config->get( $name );
		}
	}

	/**
	 * define template params
	 */
	private function getTemplate() {
		$this->template = $this->_type .'/'. $this->_name . '.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
	}

	/**
	 * get error messages
	 */
	private function getMessages() {
		$errors = array( 'warning', 'code' );

		foreach( $errors as $error ) {
			if( isset( $this->error[$error] ) ) {
				$this->data['error_' . $error] = $this->error[$error];
			}else{
				$this->data['error_' . $error] = '';
			}
		}

		unset( $errors );

		foreach( $this->data['localeLangs'] as $lang ) {
			if( isset( $this->error['code' . $lang['language_id']] ) ) {
				$this->data['error_code' . $lang['language_id']] = $this->error['code' . $lang['language_id']];
			}else{
				$this->data['error_code' . $lang['language_id']] = '';
			}
		}

		if( isset( $this->session->data['success'] ) ) {
			$this->data['success'] = $this->session->data['success'];
			unset( $this->session->data['success'] );
		}else{
			$this->data['success'] = '';
		}
	}

	/**
	 * constructs the footer
	 *
	 * Note: displaying this footer is mandatory, removing violates the license!
	 * If you do not want to display the footer, contact the author.
	 */
	private function getFooter() {
		$this->data['oxfooter']	= '<div style="text-align:center; color:#666666; margin-top:5px">'
		. $this->_fName
		. ' Module v.' . $this->_version
		. ' &copy; '
		. date( 'Y' )
		. ' by <a href="http://osworx.net" target="_blank">OSWorX</a>'
		. '</div>'
		;
	}

	/**
	 * validates user permission and checks specific module vars
	 */
	private function validate() {
		if( !$this->user->hasPermission( 'modify', $this->_type .'/'. $this->_name ) ) {
			$this->error['warning'] = $this->language->get( 'error_permission' );
		}

		if( !$this->request->post[$this->_name . '_code'] ) {
			$this->error['code'] = $this->language->get( 'error_code' );
		}

		if( !$this->error ) {
			return true;
		}else{
			return false;
		}
	}
}